﻿/***********************************************************************
 * <copyright file="SqlServerTaxTransDao.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   ThangND
 * Email:    thangnd@buca.vn
 * Website:
 * Create Date: 20 March 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessVoucher;
using DataObjects.Shared;
using DataObjects.StandartInterfaces;

namespace DataObjects.ADO.SqlServer
{
    /// <summary>
    /// SqlServerTaxTransDao
    /// </summary>
    public class SqlServerTaxTransDao : ITaxTransDao
    {
        /// <summary>
        /// Inserts the receipt voucher.
        /// </summary>
        /// <param name="journalEntryAccount">The journal entry account.</param>
        /// <returns></returns>
        public string InsertTaxTrans(TaxTrans journalEntryAccount)
        {
            const string sql = @"uspInsert_TaxTrans";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(journalEntryAccount));
        }

        /// <summary>
        /// Inserts the double journal entry account.
        /// </summary>
        /// <param name="journalEntryAccount">The journal entry account.</param>
        /// <returns></returns>
        public string InsertDoubleTaxTrans(TaxTrans journalEntryAccount)
        {
            const string sql = @"uspInsertDouble_TaxTrans";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(journalEntryAccount));
        }

        public string DeleteTaxTrans(TaxTrans journalEntryAccount)
        {
            const string sql = @"uspDelete_TaxTrans";

            object[] parms = { "@RefTypeID", journalEntryAccount.RefTypeId, "@RefID", journalEntryAccount.RefId};//, "@UnitID", journalEntryAccount.UnitId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }

        /// <summary>
        /// Gets the journal entry account for capital allocate.
        /// </summary>
        /// <param name="journalEntryAccount">The journal entry account.</param>
        /// <returns></returns>
        public List<TaxTrans> GetTaxTransForCapitalAllocate(TaxTrans journalEntryAccount)
        {

            const string sql = @"uspGet_TaxTrans_ForCapitalAllocate";
            object[] parms =
                {
                    "@AccountNumber",journalEntryAccount.AccountNumber,
                    "@CorrespondingAccountNumber", journalEntryAccount.CorrespondingAccountNumber,

                    "@FromDate",journalEntryAccount.RefDate,

                    "@JournalType", journalEntryAccount.JournalType
                };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);

        }

        /// <summary>
        /// Deletes the journal entry account.
        /// </summary>
        /// <param name="refId">The reference identifier.</param>
        /// <param name="refTypeId">The reference type identifier.</param>
        /// <returns></returns>
        public string DeleteTaxTrans(long refId, int refTypeId)
        {
            const string sql = @"uspDelete_TaxTrans";

            object[] parms = { "@RefID", refId, "@RefTypeID", refTypeId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }


        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, TaxTrans> Make = reader =>
            new TaxTrans
            {
                CustomerId = reader["CustomerID"].AsId(),

                VoucherTypeId = reader["VoucherTypeId"].AsInt(),

                VendorId = reader["VendorId"].AsInt(),
                AccountNumber = reader["AccountNumber"].AsString(),
                Description = reader["Description"].AsString(),
                RefId = reader["BudgetCategoryCode"].AsLong(),
                AmountOc = reader["AmountOc"].AsDecimal(),

                CorrespondingAccountNumber = reader["CorrespondingAccountNumber"].AsString(),
                TaxTransId = reader["TaxTransID"].AsLong(),

                JournalType = reader["JournalType"].AsInt(),
                UnitId = reader["UnitID"].AsInt(),
                Quantity = reader["Quantity"].AsInt(),
                RefDate = reader["RefDate"].AsDateTime(),
                RefDetailId = reader["RefDetailId"].AsLong(),
                RefNo = reader["RefNo"].AsString(),
                RefTypeId = reader["RefTypeId"].AsInt()
            };

        /// <summary>
        /// Takes the specified journal entry account.
        /// </summary>
        /// <param name="journalEntryAccount">The journal entry account.</param>
        /// <returns></returns>
        private static object[] Take(TaxTrans journalEntryAccount)
        {
            return new object[]  
            {
                @"TaxTransID", journalEntryAccount.TaxTransId,
                @"RefID", journalEntryAccount.RefId,
                @"RefDetailID", journalEntryAccount.RefDetailId,
                @"RefTypeID", journalEntryAccount.RefTypeId,
                @"RefNo", journalEntryAccount.RefNo,
                @"RefDate", journalEntryAccount.RefDate,

                @"Description", journalEntryAccount.Description,

                @"AccountNumber", journalEntryAccount.AccountNumber,
                @"CorrespondingAccountNumber", journalEntryAccount.CorrespondingAccountNumber,
                @"Quantity", journalEntryAccount.Quantity,
                @"JournalType", journalEntryAccount.JournalType,
                @"AmountOC", journalEntryAccount.AmountOc,

                @"CustomerID", journalEntryAccount.CustomerId,
                @"VendorID", journalEntryAccount.VendorId,
                @"VoucherTypeID", journalEntryAccount.VoucherTypeId,

                @"UnitID", journalEntryAccount.UnitId,

                @"AccountNumberVAT", journalEntryAccount.AccountNumberVat,

               @"AmountVAT", journalEntryAccount.AmountVat,

               @"TaxRate", journalEntryAccount.TaxRate

            };
        }



    }
}
